<script setup>
import { Plus } from "@element-plus/icons-vue";
import useTable from "@/utils/useTable";
import {http} from "@/api";

/**
 * if(colorPallete[0] > 200 && colorPallete[1] < 100 && colorPallete[2] < 100) {
 *         insertData.colorType = 1 // 红色
 *     } else if(colorPallete[0] < 100 && colorPallete[1] > 200 && colorPallete[2] < 100) {
 *         insertData.colorType = 2 // 绿色
 *     } else if(colorPallete[0] < 100 && colorPallete[1] < 100 && colorPallete[2] > 200) {
 *         insertData.colorType = 3 // 蓝色
 *     } else if(colorPallete[0] > 200 && colorPallete[1] > 200 && colorPallete[2] < 100) {
 *         insertData.colorType = 4 // 黄色
 *     } else if(colorPallete[0] < 100 && colorPallete[1] > 200 && colorPallete[2] > 200) {
 *         insertData.colorType = 5 // 青色
 *     } else if(colorPallete[0] > 200 && colorPallete[1] < 100 && colorPallete[2] > 200) {
 *         insertData.colorType = 6 // 紫色
 *     } else if(colorPallete[0] > 200 && colorPallete[1] > 200 && colorPallete[2] > 200) {
 *         insertData.colorType = 7 // 白色
 *     } else if(colorPallete[0] < 100 && colorPallete[1] < 100 && colorPallete[2] < 100) {
 *         insertData.colorType = 8 // 黑色
 *     } else {
 *         insertData.colorType = 9 // 其他
 *     }
 */
const colorType = [
  { name: "红色", type: "1", condition: (r, g, b) => r > 200 && g < 100 && b < 100 },
  { name: "淡红色", type: "2", condition: (r, g, b) => r > 200 && g > 100 && b > 100 },
  { name: "紫色", type: "3", condition: (r, g, b) => r > 150 && g < 100 && b > 150 },
  { name: "白色", type: "4", condition: (r, g, b) => r > 200 && g > 200 && b > 200 },
  { name: "青色", type: "5", condition: (r, g, b) => r < 100 && g > 150 && b > 150 },
  { name: "粉红色", type: "6", condition: (r, g, b) => r > 200 && g < 100 && b > 200 }
]
/**
 * 表格
 * @type {Ref<UnwrapRef<*[]>>}
 */
const {
  searchParams,
  pageNo,
  pageSize,
  total,
  changePage,
  changeSize,
  dataSource,
  search,
  reset,
  openAdd,
  openEdit,
  openDelete
} = useTable({
  requestFn: http.color,
  routerPre: 'color',
  params: {
    name: '',
    type: ''
  }
})
</script>


<template>
  <PageMain title="颜色管理">
    <el-form label-suffix="：">
      <el-row :gutter="16">
        <el-col :span="6">
          <el-form-item label="名称">
            <el-input v-model="searchParams.name" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="颜色类型">
            <el-select v-model="searchParams.type" placeholder="请选择">
              <el-option v-for="item in colorType" :label="item.name" :value="item.type" :key="item.type"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" @click="search">查询</el-button>
            <el-button @click="reset">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="add-box">
      <el-button type="primary" :icon="Plus" @click="openAdd">添加</el-button>
    </div>
    <div class="table-box">
      <el-table :data="dataSource">
        <el-table-column label="名称" prop="name">
        </el-table-column>
        <el-table-column label="颜色类型" prop="region">
          <template #default="scope">
            <div style="display: flex; align-items: center">
              <span>{{ colorType.find(e => e.type == scope.row.type)?.name || scope.row.type}}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="描述" prop="desc">
        </el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button type="text" @click="openEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="openDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[10, 50, 100, 200]"
          background
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="changeSize"
          @current-change="changePage"
      />
    </div>
  </PageMain>
</template>

<style scoped lang="scss">

</style>
